package com.raising.modules.cms.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.cms.entity.CmsArticle;
import com.raising.modules.cms.entity.CmsCategory;
import com.raising.modules.cms.service.CmsArticleService;
import com.raising.modules.cms.service.CmsCategoryService;
import com.raising.modules.sys.entity.Organization;
import com.raising.modules.sys.service.OrganizationService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JDateUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 文章表 控制器
 * @author luxy
 * @createTime 2018-01-20 19:12:39
 */
@Controller
@RequestMapping("${adminPath}/cms/cmsArticle")
public class CmsArticleController extends BaseController {

    @Autowired
    private CmsArticleService cmsArticleService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private CmsCategoryService cmsCategoryService;

    /**
     * 跳转至列表页面
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        olog("进入文章管理页面");
        //栏目类型下拉框
        CmsCategory cmsCategory = new CmsCategory();
        model.addAttribute("cmsCategoryList", cmsCategoryService.getList(cmsCategory));
        return "cms/cmsArticle/list";
    }

    /**
     * 加载列表分页数据
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(CmsArticle cmsArticle,Page<CmsArticle> page)throws Exception {
        page.setEntity(cmsArticle);
        page.setOrderBy(" a.id desc ");
        return ResultVo.success().data(cmsArticleService.getPage(page));
    }

    /**
     * 跳转新增页面
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) throws Exception{
        model.addAttribute("cmsArticle", new CmsArticle());
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));

        Organization organization = new Organization();
        organization.setStatus(StaticParam.VALID_STRING);
        organization.setAvailable(true);
        model.addAttribute("organizationList",organizationService.getList(organization));

        //栏目类型下拉框
        CmsCategory cmsCategory = new CmsCategory();
        model.addAttribute("cmsCategoryList", cmsCategoryService.getList(cmsCategory));

        model.addAttribute("showDateDefault", JDateUtils.getDate());
        return "cms/cmsArticle/add";
    }

    /**
     * 执行新增操作
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:create")
    @RequestMapping(value = "/create.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo createJson(CmsArticle cmsArticle) throws Exception{
        setCmsArticlePropertiesToZero(cmsArticle);
        cmsArticle.setStatus(StaticParam.VALID_STRING);
        cmsArticleService.insert(cmsArticle);
        olog("新增文章["+ cmsArticle.getTitle() +"]",cmsArticle.toString());
        return ResultVo.success().desc("新增成功").data(cmsArticle.getId());
    }

    /**
     * 跳转更新页面
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String id, Model model) throws Exception{
        model.addAttribute("cmsArticle", cmsArticleService.get(id));
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));

        Organization organization = new Organization();
        organization.setStatus(StaticParam.VALID_STRING);
        organization.setAvailable(true);
        model.addAttribute("organizationList",organizationService.getList(organization));
        //栏目类型下拉框
        CmsCategory cmsCategory = new CmsCategory();
        model.addAttribute("cmsCategoryList", cmsCategoryService.getList(cmsCategory));
        return "cms/cmsArticle/edit";
    }

    /**
     * 执行更新操作
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:update")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo updateJson(CmsArticle cmsArticle) throws Exception{
        setCmsArticlePropertiesToZero(cmsArticle);
        cmsArticleService.update(cmsArticle);
        olog("修改文章["+ cmsArticle.getTitle() +"]",cmsArticle.toString());
        return ResultVo.success().desc("更新成功").data(cmsArticle.getId());
    }

    /**
     * 执行删除操作
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:delete")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo deleteJson(String id) throws Exception{
        return cmsArticleService.delete(id);
    }

    /**
     * 跳转查看页面
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    @RequiresPermissions("cms:cmsArticle:view")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(String id, Model model) throws Exception{
        CmsArticle cmsArticle = cmsArticleService.get(id);
        olog("查看文章["+cmsArticle.getTitle()+"]详情");
        model.addAttribute("cmsArticle", cmsArticle);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));

        Organization organization = new Organization();
        organization.setStatus(StaticParam.VALID_STRING);
        organization.setAvailable(true);
        model.addAttribute("organizationList",organizationService.getList(organization));
        return "cms/cmsArticle/view";
    }


    /**
     * 统一置为0
     * @author luxy
     * @createTime 2018-01-20 19:12:39
     */
    public void setCmsArticlePropertiesToZero(CmsArticle cmsArticle){
        if(JStringUtils.isBlank(cmsArticle.getHits())){
            cmsArticle.setHits("0");
        }
    }

}
